package com.owen.springboot.smartparkingms.module.app.controller;

import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.DateUtils;
import com.owen.springboot.smartparkingms.common.util.ShiroUtils;
import com.owen.springboot.smartparkingms.module.app.entity.Park;
import com.owen.springboot.smartparkingms.module.app.service.IParkService;
import com.owen.springboot.smartparkingms.module.sys.entity.SysUser;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/app/park")
public class ParkController {

    @Resource
    IParkService parkService;

    /**
     * 列表
     */
    @PostMapping("list")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result list(Park park){
        return parkService.listPage(park);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result save(@RequestBody Park park){
        if (park.getParkId() == null) {
            park.setGmtCreate(DateUtils.getTimestamp());
            park.setUserIdCreate(ShiroUtils.getUserId());
        }else {
            park.setGmtModified(DateUtils.getTimestamp());
        }
        parkService.saveOrUpdate(park);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result delete(Long id){
        parkService.removeById(id);
        return Result.ok();
    }

    /**
     * 获取
     */
    @PostMapping("get")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public Result get(Long id){
        Park park = parkService.getById(id);
        if(park != null){
            return Result.ok(park);
        }else {
            return Result.ok(new Park());
        }
    }

    /**
     * 列表
     */
    @PostMapping("/selectByUser")
    public Result selectByUser(){
        List<Park> list = null;
        SysUser user =ShiroUtils.getUserEntity();
        if(user.getUsername().equals("admin")){
            list = parkService.list();
        }else{
            list = parkService.selectByUserId(user.getUserId());
        }
        return Result.ok(list);
    }

    @PostMapping("/select")
    public Result select(){
        return Result.ok(parkService.list());
    }

}
